<html>
<head><meta charset="utf-8"><title>impl trait allows returning different closures · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html">impl trait allows returning different closures</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244942979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244942979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244942979">(Jul 05 2021 at 14:31)</a>:</h4>
<p>This code <strong>compiles</strong> in Rust 1.53:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">returns_closure</span><span class="p">(</span><span class="n">maybe</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="nb">Fn</span><span class="p">(</span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">maybe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="n">y</span><span class="o">|</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>We had expected that it would not, because we are dynamically selecting which closure to return.</p>
<p>Looking at the MIR, the closures have been promoted to functions and the <code>impl Trait</code> becomes a function pointer:</p>
<div class="codehilite"><pre><span></span><code>fn returns_closure(_1: bool) -&gt; fn(i32) -&gt; i32 {
</code></pre></div>
<p>Is this deliberate or a leaky optimization or something?</p>



<a name="244943278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244943278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244943278">(Jul 05 2021 at 14:33)</a>:</h4>
<p>Because this automatically creates dynamic dispatch, which feels surprising</p>



<a name="244943418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244943418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244943418">(Jul 05 2021 at 14:34)</a>:</h4>
<p>Isn't this the same as (with some knowledge of the actual signature)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">maybe</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="w"></span>
<span class="p">}</span><span class="k">else</span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="o">|</span><span class="n">y</span><span class="o">|</span><span class="n">y</span><span class="o">+</span><span class="mi">2</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>if statements trigger coercions to unify the type produced in each case.</p>



<a name="244943623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244943623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244943623">(Jul 05 2021 at 14:36)</a>:</h4>
<p>(I've used this to trigger decay from fn-item to fn-pointer because the whole fn-item design is absolutely brilliant and has no flaws whatsoever)</p>



<a name="244943751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244943751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244943751">(Jul 05 2021 at 14:37)</a>:</h4>
<p>That seems the same, but it's still new(ish). e.g. Rust 1.34:</p>
<div class="codehilite"><pre><span></span><code>error[E0308]: if and else have incompatible types
 --&gt; src/main.rs:5:9
  |
2 | /     if maybe {
3 | |         |x| x + 1
  | |         --------- expected because of this
4 | |     } else {
5 | |         |y| y + 2
  | |         ^^^^^^^^^ expected closure, found a different closure
6 | |     }
  | |_____- if and else have incompatible types
  |
  = note: expected type `[closure@src/main.rs:3:9: 3:18]`
             found type `[closure@src/main.rs:5:9: 5:18]`
</code></pre></div>



<a name="244943935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244943935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244943935">(Jul 05 2021 at 14:39)</a>:</h4>
<p>Ditto 1.40</p>



<a name="244944133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244944133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244944133">(Jul 05 2021 at 14:41)</a>:</h4>
<p>Ok, this started working in 1.45</p>



<a name="244944331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244944331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244944331">(Jul 05 2021 at 14:42)</a>:</h4>
<p>I don't see anything obvious in RELEASES.md</p>



<a name="244944586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244944586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244944586">(Jul 05 2021 at 14:45)</a>:</h4>
<p>Probably inference related.</p>



<a name="244944636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244944636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244944636">(Jul 05 2021 at 14:45)</a>:</h4>
<p>I stand by this statement though:</p>
<blockquote>
<p>Because this automatically creates dynamic dispatch, which feels surprising</p>
</blockquote>



<a name="244945076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244945076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244945076">(Jul 05 2021 at 14:50)</a>:</h4>
<p>Yeah. I think it's a good feature though, as it's the only way I know of to force a coercion in a generic context (which I have needed to validate functions used to initialize a type-erased syscall array).</p>



<a name="244946398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244946398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244946398">(Jul 05 2021 at 15:02)</a>:</h4>
<p>I'm pretty sure this is unintentional; I don't recall T-lang FCPs to this effect on this time frame. <span class="user-mention" data-user-id="116155">@Jake Goulding</span> could you file an issue about this?</p>



<a name="244946450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244946450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244946450">(Jul 05 2021 at 15:03)</a>:</h4>
<p>Can do. Pinging t-lang?</p>



<a name="244947192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947192">(Jul 05 2021 at 15:10)</a>:</h4>
<p>hmm, I recall this feature being added</p>



<a name="244947249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947249">(Jul 05 2021 at 15:10)</a>:</h4>
<p>it seems a bit sketchy though, coercions normally require a known target type, but this looks like it does some sort of pseudo-subtyping by computing a common coercion target?</p>



<a name="244947326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947326">(Jul 05 2021 at 15:11)</a>:</h4>
<p>that's <a href="https://github.com/rust-lang/rust/issues/71599">#71599</a></p>



<a name="244947373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947373">(Jul 05 2021 at 15:12)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/71599">https://github.com/rust-lang/rust/pull/71599</a></p>



<a name="244947387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947387">(Jul 05 2021 at 15:12)</a>:</h4>
<p>ah, yes</p>



<a name="244947432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947432">(Jul 05 2021 at 15:13)</a>:</h4>
<p>I don't think the discussion at the time considered the impl trait relationship with function pointers, though I suppose it is sort of "obvious" in hindsight.</p>



<a name="244947441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947441">(Jul 05 2021 at 15:13)</a>:</h4>
<p>I think it's a good point that this was already happening for function references before</p>



<a name="244947474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947474">(Jul 05 2021 at 15:13)</a>:</h4>
<p>this just extended that a bit</p>



<a name="244947567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/244947567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#244947567">(Jul 05 2021 at 15:14)</a>:</h4>
<p>I opened <a href="https://github.com/rust-lang/rust/issues/86885">https://github.com/rust-lang/rust/issues/86885</a>, FWIW.</p>



<a name="245267693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245267693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245267693">(Jul 08 2021 at 03:05)</a>:</h4>
<p>This doesn't really look like dynamic dispatch, because it's a function pointer not a trait object (ex. not <code>&amp;dyn Fn(i32) -&gt; i32</code>). There's no vtable AFAIK.</p>



<a name="245340247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245340247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245340247">(Jul 08 2021 at 16:52)</a>:</h4>
<p>A vtable, while adding another layer of memory indirection, is mainly just a bunch of function pointers.</p>



<a name="245344479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245344479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245344479">(Jul 08 2021 at 17:26)</a>:</h4>
<p>So how much better can you get than a raw function pointer? At some point you need to jump to the function's address, and a raw function pointer seems like the fastest way to provide that functionality.</p>



<a name="245344880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245344880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245344880">(Jul 08 2021 at 17:29)</a>:</h4>
<p>Not performing dynamic dispatch at all is usually faster</p>



<a name="245345269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245345269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245345269">(Jul 08 2021 at 17:32)</a>:</h4>
<p>a direct call is still faster than an indirect call on many platforms</p>



<a name="245421169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245421169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245421169">(Jul 09 2021 at 09:30)</a>:</h4>
<p>Not to mention that direct calls can be optimized better by LLVM</p>



<a name="245497504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245497504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245497504">(Jul 09 2021 at 21:01)</a>:</h4>
<p>I'm also unusual in that I classify <em>enums</em> as dynamic dispatch. I've gotten weird looks from that in the past, but I stand by it <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="245634079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245634079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245634079">(Jul 12 2021 at 00:00)</a>:</h4>
<p>Switching over an enum's discriminant is definitely dynamic, and if used to dispatch the kind of logic to use afterwards, then it does qualify as dynamic dispatch <span aria-label="100" class="emoji emoji-1f4af" role="img" title="100">:100:</span> just not a function pointer / virtual method-based one <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="245634338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245634338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245634338">(Jul 12 2021 at 00:06)</a>:</h4>
<p>okay, but then so is <em>any</em> data-dependent control flow</p>



<a name="245676555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245676555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245676555">(Jul 12 2021 at 12:04)</a>:</h4>
<p>Makes sense to me... the code is dispatched dynamically. <em>I</em> don't see a problem with that, but it's not an area where I'm often interacting with other people.</p>



<a name="245676653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245676653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245676653">(Jul 12 2021 at 12:05)</a>:</h4>
<p>I think it's valuable to not automatically go "dynamic dispatch is bad" (which I think is unfortunately prevalent in Rust world), but rather to evaluate it in comparison to what other choices are possible.</p>



<a name="245676725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/impl%20trait%20allows%20returning%20different%20closures/near/245676725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/impl.20trait.20allows.20returning.20different.20closures.html#245676725">(Jul 12 2021 at 12:06)</a>:</h4>
<p>And in comparison to the performance characteristics of any alternatives.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>